package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@com.alibaba.dubbo.config.annotation.Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper TbSpecificationOptionMapper;
    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;

    @Override
    public List<TbSpecification> findAll(String specName) {

        List<TbSpecification> list = tbSpecificationMapper.findAll(specName);
        return list;
    }

    @Override
    public void insertSpec(TbSpecification tbSpecification, List<TbSpecificationOption> options) {

            int i = tbSpecificationMapper.insertSpeReturnId(tbSpecification);

        /*    for(TbSpecificationOption option : options){
                option.setSpecId(tbSpecifi cation.getId());
            }*/
            TbSpecificationOptionMapper.insertBath(tbSpecification.getId(),options);

    }

    @Override
    public List<TbSpecification> selectAll() {
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(null);
        return tbSpecifications;
    }

    @Override
    public List<OptionVO> selectData(Long id) {
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(id);

        List<OptionVO> optionVOList = JSONObject.parseArray(tbTypeTemplate.getSpecIds(),OptionVO.class);

        for (OptionVO optionVO : optionVOList) {
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(optionVO.getId());
            List<TbSpecificationOption>  options =  TbSpecificationOptionMapper.selectByExample(example);
            optionVO.setSpecificationOptions(options);
        }
        return optionVOList;
    }
}
